package com.ecarx.camera.worker.camera.dvr;

import com.ecarx.camera.constant.Constants;
import com.ecarx.camera.data.ThumbnailData;
import com.ecarx.camera.utils.LogUtils;
import com.ecarx.camera.worker.camera.dvr.impl.DvrCarPropertyAPIForRImpl;
import com.ecarx.camera.worker.camera.dvr.impl.DvrDebugAPIImpl;
import com.ecarx.camera.worker.camera.dvr.impl.DvrNativeAPPAPIImpl;
import com.ecarx.camera.worker.config.ConfigManager;

/**
 * Dvr管理类（封装所有Dvr相关接口）
 */
public class DvrManager implements IDvrAPI, IDvrAPI.IDvrThumbnailAPI, IDvrAPI.IDvrConfigAPI {

    private static final String TAG = "DvrManager";

    private final IDvrAPI mDvrAPI;
    private final IDvrThumbnailAPI mDvrThumbnailAPI;
    private final IDvrConfigAPI mDvrConfigAPI;

    private boolean isFirstInit = true;

    private static class Holder {
        private static final DvrManager sInstance = new DvrManager();
    }

    DvrManager() {
        if (Constants.USE_DVR_DEBUG_IMPL) {
            mDvrAPI = new DvrDebugAPIImpl();
            mDvrThumbnailAPI = new DvrDebugAPIImpl.DvrThumbnailAPIDebugImpl();
            mDvrConfigAPI = new DvrDebugAPIImpl.DvrConfigAPIDebugImpl();
        } else {
            DvrCarPropertyAPIForRImpl dvrCarPropertyAPIForR = new DvrCarPropertyAPIForRImpl();
            mDvrAPI = dvrCarPropertyAPIForR;
            mDvrConfigAPI = dvrCarPropertyAPIForR;
            mDvrThumbnailAPI = new DvrNativeAPPAPIImpl();
        }
    }

    public static DvrManager get() {
        return Holder.sInstance;
    }

    @Override
    public void init() {
        mDvrAPI.init();

        // 开机后，如果开机前DVR一般循环录像中，则继续保持一般循环录像中的状态
        if (isFirstInit) {
            boolean lastGeneralRecording = ConfigManager.getInstance().isExteriorGeneralRecording();
            LogUtils.d(TAG, "init, lastGeneralRecording:" + lastGeneralRecording);
            if (lastGeneralRecording) {
                mDvrAPI.startGeneralRecord();
            }
            isFirstInit = false;
        }

    }

    @Override
    public void destroy() {
        mDvrAPI.destroy();
    }

    @Override
    public boolean registerDvrStateWatcher(IDvrStateWatcher watcher) {
        return mDvrAPI.registerDvrStateWatcher(watcher);
    }

    @Override
    public void unregisterDvrStateWatcher(IDvrStateWatcher watcher) {
        mDvrAPI.unregisterDvrStateWatcher(watcher);
    }

    @Override
    public boolean registerSdcardStateWatcher(ISdcardStateWatcher watcher) {
        return mDvrAPI.registerSdcardStateWatcher(watcher);
    }

    @Override
    public void unregisterSdcardStateWatcher(ISdcardStateWatcher watcher) {
        mDvrAPI.unregisterSdcardStateWatcher(watcher);
    }

    @Override
    public boolean registerOperateResultsWatcher(IOperateResultsWatcher watcher) {
        return mDvrAPI.registerOperateResultsWatcher(watcher);
    }

    @Override
    public void unregisterOperateResultsWatcher(IOperateResultsWatcher watcher) {
        mDvrAPI.unregisterOperateResultsWatcher(watcher);
    }

    @Override
    public boolean openDvr() {
        return mDvrAPI.openDvr();
    }

    @Override
    public boolean exitDvr() {
        return mDvrAPI.exitDvr();
    }

    @Override
    public boolean startGeneralRecord() {
        return mDvrAPI.startGeneralRecord();
    }

    @Override
    public boolean startEmergencyRecord() {
        return mDvrAPI.startEmergencyRecord();
    }

    @Override
    public boolean pauseGeneralRecord() {
        return mDvrAPI.pauseGeneralRecord();
    }

    @Override
    public boolean doCapture() {
        return mDvrAPI.doCapture();
    }

    @Override
    public boolean doDvrFactoryReset() {
        return mDvrAPI.doDvrFactoryReset();
    }

    @Override
    public boolean doDvrSdcardFormat() {
        return mDvrAPI.doDvrSdcardFormat();
    }

    @Override
    public boolean wakeupEthernet() {
        return mDvrAPI.wakeupEthernet();
    }

    @Override
    public boolean isDvrSdcardExists() {
        return mDvrAPI.isDvrSdcardExists();
    }

    @Override
    public boolean isDvrGeneralRecording() {
        return mDvrAPI.isDvrGeneralRecording();
    }

    @Override
    public boolean isDvrEmergencyRecording() {
        return mDvrAPI.isDvrEmergencyRecording();
    }

    @Override
    public ThumbnailData getDvrLatestThumbnail() {
        return mDvrThumbnailAPI.getDvrLatestThumbnail();
    }

    @Override
    public String getDvrSoftWareVersion() {
        return mDvrConfigAPI.getDvrSoftWareVersion();
    }

    @Override
    public String getDvrHardWareVersion() {
        return mDvrConfigAPI.getDvrHardWareVersion();
    }

    @Override
    public boolean isDvrDeviceSupported() {
        return mDvrConfigAPI.isDvrDeviceSupported();
    }

    @Override
    public int getDvrGeneralRecordResolution() {
        return mDvrConfigAPI.getDvrGeneralRecordResolution();
    }

    @Override
    public int getDvrGeneralRecordDuration() {
        return mDvrConfigAPI.getDvrGeneralRecordDuration();
    }

    @Override
    public boolean getDvrGeneralRecordMicMute() {
        return mDvrConfigAPI.getDvrGeneralRecordMicMute();
    }

    @Override
    public boolean setDvrGeneralRecordResolution(int resolution) {
        return mDvrConfigAPI.setDvrGeneralRecordResolution(resolution);
    }

    @Override
    public boolean setDvrGeneralRecordDuration(int duration) {
        return mDvrConfigAPI.setDvrGeneralRecordDuration(duration);
    }

    @Override
    public boolean setDvrGeneralRecordMicMute(boolean mute) {
        return mDvrConfigAPI.setDvrGeneralRecordMicMute(mute);
    }

    @Override
    public boolean registerDvrConfigWatcher(IDvrConfigWatcher watcher) {
        return mDvrConfigAPI.registerDvrConfigWatcher(watcher);
    }

    @Override
    public void unregisterDvrConfigWatcher(IDvrConfigWatcher watcher) {
        mDvrConfigAPI.unregisterDvrConfigWatcher(watcher);
    }

    @Override
    public int getSdcardUsedPercentage() {
        return mDvrConfigAPI.getSdcardUsedPercentage();
    }

}
